from mydefine import *

def pullType(head_recved, db):
    block_size = head_recved['block_size']
    category = getModelCategory(head_recved['category'])
    assetType = head_recved['assetType']
    head_tosend = {}
    if (0 == len(block_size)):
        head_tosend = {"type": RESPONSE_ERROR, "status": "没有数据！"}
    else:
        status, rows = db.sqlexe('select name from tb_type where assetType = %s and category = "%s"' % (assetType, category))
        models = {"titles": ['name'], "values": [name for (name) in rows]}
        print(models)
        head_tosend = {"type": RESPONSE_PULLTYPE, "status":status, "models": models}
    return head_tosend


def pullTags(head_recved, db):
    block_size = head_recved['block_size']
    category = getModelCategory(head_recved['category'])
    assetType = head_recved['assetType']
    head_tosend = {}
    if (0 == len(block_size)):
        head_tosend = {"type": RESPONSE_ERROR, "status": "没有数据！"}
    else:
        status, rows = db.sqlexe('select name from tb_tags where assetType = %s and category = "%s"' % (assetType, category))
        models = {"titles": ['name'], "values": [name for (name) in rows]}
        print(models)
        head_tosend = {"type": RESPONSE_PULLTAGS, "status":status, "models": models}
    return head_tosend


def pushType(head_recved, db):
    block_size = head_recved['block_size']
    category = getModelCategory(head_recved['category'])
    assetType = head_recved['assetType']
    values = head_recved['values']
    head_tosend = {}
    # if (0 == len(block_size)):
    #     head_tosend = {"type": RESPONSE_ERROR, "status": "没有数据！"}
    # else:
    # 首先查询已有类型
    status, pre_types = db.sqlexe('select name from tb_type where assetType = %s and category = "%s"' % (assetType, category))
    cur_types = [value['name'] for value in values]
    pre_set = set(t[0] for t in pre_types)
    cur_set = set(cur_types)
    added_types = cur_set.difference(pre_set)
    removed_types = pre_set.difference(cur_set)
    if removed_types.__len__() > 0:
        l_removed = [[t] for t in removed_types]
        if ASSET_MODEL == assetType:
            status, _ = db.sqlexe(f'update tb_model set type="" where type=? and category="{category}"', l_removed)
        elif ASSET_BVH == assetType:
            status, _ = db.sqlexe(f'update tb_bvh set type="" where type=? and category="{category}"', l_removed)
        elif ASSET_EFFECT == assetType:
            status, _ = db.sqlexe(f'update tb_effect set type="" where type=? and category="{category}"', l_removed)
        status, _ = db.sqlexe(f'delete from tb_type where assetType="{assetType}" and category="{category}" and name=?', l_removed)
    if added_types.__len__() > 0:
        l_added = [[t] for t in added_types]
        status, _ = db.sqlexe(f'insert into tb_type (name,category,assetType) VALUES (?,"{category}",{assetType})', l_added)
    models = {"titles": [], "values": []}
    head_tosend = {"type": RESPONSE_PUSHTYPE, "status":status, "models": models}
    return head_tosend


def pushTags(head_recved, db):
    block_size = head_recved['block_size']
    category = getModelCategory(head_recved['category'])
    assetType = head_recved['assetType']
    values = head_recved['values']
    head_tosend = {}
    # if (0 == len(block_size)):
    #     head_tosend = {"type": RESPONSE_ERROR, "status": "没有数据！"}
    # else:
    # 首先查询已有类型
    status, pre_tags = db.sqlexe('select name from tb_tags where assetType = %s and category = "%s"' % (assetType, category))
    cur_tags = [value['name'] for value in values]
    pre_set = set(t[0] for t in pre_tags)
    cur_set = set(cur_tags)
    added_tags = cur_set.difference(pre_set)
    removed_tags = pre_set.difference(cur_set)
    if removed_tags.__len__() > 0:
        l_removed = [[t] for t in removed_tags]
        if ASSET_MODEL == assetType:
            status, _ = db.sqlexe(f'update tb_model set tags="" where tags=? and category="{category}"', l_removed)
        elif ASSET_BVH == assetType:
            status, _ = db.sqlexe(f'update tb_bvh set tags="" where tags=? and category="{category}"', l_removed)
        elif ASSET_EFFECT == assetType:
            status, _ = db.sqlexe(f'update tb_effect set tags="" where tags=? and category="{category}"', l_removed)
        status, _ = db.sqlexe(f'delete from tb_tags where assetType="{assetType}" and category="{category}" and name=?', l_removed)
    if added_tags.__len__() > 0:
        l_added = [[t] for t in added_tags]
        status, _ = db.sqlexe(f'insert into tb_tags (name,category,assetType) VALUES (?,"{category}",{assetType})', l_added)
    models = {"titles": [], "values": []}
    head_tosend = {"type": RESPONSE_PUSHTAGS, "status":status, "models": models}
    return head_tosend

